package com.chenxin.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chenxin.common.exception.ServiceException;
import com.chenxin.wms.domain.LocationEntity;
import com.chenxin.wms.domain.po.LocationInfo;
import com.chenxin.wms.domain.vo.LocationVO;
import com.chenxin.wms.mapper.LocationMapper;
import com.chenxin.wms.service.ILocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 货位资料Service业务层处理
 *
 * @author gry
 * @date 2025-04-14
 */
@Service
public class LocationServiceImpl implements ILocationService {
    @Autowired
    private LocationMapper locationMapper;

    /**
     * 查询货位资料列表
     *
     * @param locationEntity 货位资料
     * @return 货位资料
     */
    @Override
    public List<LocationVO> selectLocationList(LocationEntity locationEntity) {
        return locationMapper.selectLocationViewList(locationEntity);
    }

    @Override
    public LocationEntity selectToLocationByZoneId(Long zoneId) {
        QueryWrapper<LocationEntity> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.lambda()
                .eq(LocationEntity::getZoneId, zoneId) // 当前位置库区的id
                .eq(LocationEntity::getLocationStatus, "0") // 空闲货位
                .eq(LocationEntity::getIsActive, "0") // 启用状态
                .eq(LocationEntity::getLocationType, "HW") // 实体货位
                .orderByAsc(LocationEntity::getUpdateDate)
                .last("limit 1");
        LocationEntity locationEntity = locationMapper.selectOne(locationQueryWrapper);

        if (ObjectUtils.isEmpty(locationEntity)) {
            throw new ServiceException("没有空闲的货位！");
        }
        return locationEntity;
    }
}
